package solvers;

/**
 * Created by IntelliJ IDEA.
 * User: dmitry
 * Date: 07.04.12
 * Time: 21:49
 * To change this template use File | Settings | File Templates.
 */
public final class SimpleHybridiser implements Hybridiser{
    private static final SimpleHybridiser INSTANCE = new SimpleHybridiser();

    public static SimpleHybridiser getInstance() {
        return INSTANCE;
    }
    
    private SimpleHybridiser() {
        
    }
    
    
    @Override
    public int[] hybridise(int[] x, int[] y, int p)  {
        int[] z = new int[x.length];
        for(int i = 0; i < x.length; i++) {
            if(i < x.length / 2)
                z[i] = x[i];
            else
                z[i] = y[i];
        }

        for(int i = 0; i < x.length / 2; i++) {
            if(z[i] == 1) {
                p--;
                if(p < 0) {
                    z[i] = 0;
                }
            }
            if(z[x.length - i - 1] == 1) {
                p--;
                if(p < 0) {
                    z[x.length - i - 1] = 0;
                }
            }
        }

        return z;
    }
}
